//
//  WMH_NewsPicList_ViewController.m
//  EpointFrame5
//
//  Created by AndLi on 15/4/2.
//  Copyright (c) 2015年 Epoint. All rights reserved.
//  图片新闻

#import "WMH_NewsPicList_ViewController.h"
#import "PicInfoListModel.h"
#import "NewsPicCell.h"
#import "MJRefresh.h"
#import "EpointWebInfoDetailViewController.h"

#define StartIndex 0

@interface WMH_NewsPicList_ViewController ()
{
    NSMutableArray *dataArray;
    UITableView *tv;
    UISearchBar *sb;
    UISearchDisplayController *searchController;
    MJRefreshFooterView *_footer;
    MJRefreshHeaderView *_header;
    //列表索引
    int pageIndex;
    
    //每页列表数量
    int listPageCount;
    
    //搜索关键字
    NSString *keyWork;
    
    //字体大小
    double list_title_fontsize;
    double list_date_fontsize;
    //标题字数
    int list_title_length;
    
}
@end

@implementation WMH_NewsPicList_ViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    pageIndex       = StartIndex;
    listPageCount   = 20;
    keyWork = @"";
    
    list_title_fontsize = [[DBFrameService getConfigValue:WMH_InfoList_Title_FontSize]  floatValue];
    list_date_fontsize  = [[DBFrameService getConfigValue:WMH_InfoList_Date_FontSize]   floatValue];
    list_title_length   = [[DBFrameService getConfigValue:WMH_InfoList_Title_Length]    floatValue];
    
    dataArray = [[NSMutableArray alloc]init];
    
    tv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, AppWidth, AppHeight-SystemTakeHeight)];
    tv.rowHeight = 75;
    tv.dataSource = self;
    tv.delegate = self;
    
    [self.view addSubview:tv];
    
    [self getData:pageIndex taskid:2];
    [self addHeader];
    [self addFooter];
}

- (void)addFooter
{
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = tv;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        pageIndex++;
        [self getData:pageIndex taskid:1];
    };
    _footer = footer;
}

-(void)addHeader
{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = tv;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        pageIndex = StartIndex;
        [self getData:pageIndex taskid:2];
    };
    _header = header;
}

-(void)getData:(int)_index taskid:(int)taskid
{
    [self showMBPHUD];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:[NSString stringWithFormat:@"%i",_index]          forKey:@"CurrentPageIndex"];
    [params setObject:[NSString stringWithFormat:@"%i",listPageCount]   forKey:@"PageSize"];
    [params setObject:CateNum_NEWSPIC                                   forKey:@"CateNum"];
    [params setObject:@"0"                                              forKey:@"IsHeadNews"];
    [params setObject:@""                                               forKey:@"Title"];
    [self startTask:@"Task_GetInfoListWithImageGuid" withParams:params withTaskId:taskid];
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    [self hideMBPHUD];
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        [_footer endRefreshing];
        [_header endRefreshing];
        return;
    }
    
    if (taskId==1) {
        
        NSMutableArray *listData = [arr objectAtIndex:2];
        
        for (PicInfoListModel *m in listData) {
            [dataArray addObject:m];
        }
        [tv reloadData];
        [_footer endRefreshing];
        if (listData.count<listPageCount&&keyWork.length==0) {
            [_footer removeFromSuperview];
        }
    }
    else if (taskId==2)
    {
        [_footer removeFromSuperview];
        [self addFooter];
        [dataArray removeAllObjects];
        NSMutableArray *listData = [arr objectAtIndex:2];
        
        for (PicInfoListModel *m in listData) {
            [dataArray addObject:m];
        }
        [_header endRefreshing];
        [tv reloadData];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *WebinfolistItem = @"NewsPicCell";
    NewsPicCell *cell = (NewsPicCell *)[tableView dequeueReusableCellWithIdentifier:WebinfolistItem];
    
    if(cell==nil){
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"NewsPicCell"
                                                    owner:self
                                                  options:nil];
        cell = [nib objectAtIndex:0];
        
    }
    NSUInteger row = [indexPath row];
    PicInfoListModel *info = [dataArray objectAtIndex:row];
    
    NSURL *imageurl;
    UIImage *imageView;
    imageurl = [NSURL URLWithString:[info.BigImgUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    imageView = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];
    cell.pic1ImageView.image = imageView;
    
    imageurl = [NSURL URLWithString:[info.SmallImgUrl1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    imageView = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];
    cell.pic2ImageView.image = imageView;
    
    imageurl = [NSURL URLWithString:[info.SmallImgUrl2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    imageView = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];
    cell.pic3ImageView.image = imageView;
    
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",info.Title];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 227;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PicInfoListModel *m = [dataArray objectAtIndex:indexPath.row];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:m.InfoID          forKey:@"InfoID"];
    [params setObject:CateNum_NEWSPIC   forKey:@"CateNum"];
    [params setObject:@"0"              forKey:@"IsNeedUrl"];
    WMH_NewsDetail_ViewController *view = [[WMH_NewsDetail_ViewController alloc]init];
    view.passParam = params;
    [self.navigationController pushViewController:view animated:YES];
}

@end


